<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class search_class extends AWS_MODEL
{
	public function get_all_result($q, $limit = 20)
	{		
		if ($users = $this->search_users($q, $limit))
		{
			foreach($users as $key=>$val)
			{
				if (in_array($val['user_name'],$q))
				{
					$users[$key]['exactly_match']=1;
				}
				else
				{
					$users[$key]['exactly_match']=0;
				}
					
				$users[$key]['s_index']=3;//优先级
			}
		}
			
		if ($questions = $this->search_questions($q, $limit))
		{
			foreach($questions as $key=>$val)
			{
				if (in_array($val['question_content'], $q))
				{
					$questions[$key]['exactly_match']=1;
				}
				else
				{
					$questions[$key]['exactly_match']=0;	
				}
				
				$questions[$key]['s_index']=1;//优先级
			}
		}

		if ($topics = $this->search_topics($q, $limit))
		{
			foreach($topics as $key=>$val)
			{
				if (in_array($val['topic_title'], $q))
				{
					$topics[$key]['exactly_match']=1;
				}
				else
				{
					$topics[$key]['exactly_match']=0;
				}
					
				$topics[$key]['s_index'] = 2;//优先级
			}
		}			

		$result = array_merge((array)$users, (array)$questions, (array)$topics);
		
		$result = aasort($result, array(
				'-exactly_match', '-s_index'
		));
		
		return $result;
	}
	
	public function search_users($q, $limit = 20)
	{
		
		if (is_array($q))
		{
			foreach ($q AS $key)
			{
				$where[] = "user_name LIKE '%" . $this->quote($key) . "%'";
			}
		}
		else
		{
			$where[] = "user_name LIKE '%" . $this->quote($q) . "%'";
		}
		
		
		return $this->query_all("SELECT uid, last_login, user_name FROM {$this->get_table("users")} WHERE (" . implode($where, ' AND ') . ")", $limit);
	}

	public function search_topics($q, $limit = 20)
	{
		if (is_array($q))
		{
			foreach ($q AS $key)
			{
				$where[] = "topic_title LIKE '%" . $this->quote($key) . "%'";
			}
		}
		else
		{
			$where[] = "topic_title LIKE '%" . $this->quote($q) . "%'";
		}
		
		return $this->fetch_all('topic', implode($where, ' AND '), ' length(topic_title) ASC ' , $limit);
	}
	
	public function search_questions($q, $limit = 20)
	{
		if (is_array($q))
		{
			foreach ($q AS $key)
			{
				$where[] = "question_content LIKE '%" . $this->quote($key) . "%'";
			}
		}
		else
		{
			$where[] = "question_content LIKE '%" . $this->quote($q) . "%'";
		}
		
		return $this->fetch_all('question', implode($where, ' AND '), null, $limit);
	}
	
	public function search($q, $search_type, $limit = 20)
	{		
		if (! in_array($search_type, array(
			'all', 
			'user', 
			'topic', 
			'topic_add', 
			'question'
		)))
		{
			$search_type = 'all';
		}
		
		$q = (array)explode(' ', str_replace('  ', ' ', trim($q)));
		
		foreach ($q AS $key => $val)
		{
			if (cjk_strlen($val) == 1)
			{
				unset($q[$key]);
			}
		}
		
		if (sizeof($q) == 0)
		{
			return array();
		}
		
		$data = array();
		
		switch ($search_type)
		{
			case 'all' :
				$result_list = $this->get_all_result($q, $limit);
				break;
			
			case 'user' :
				$result_list = $this->search_users($q, $limit);
				break;
			
			case 'topic' :
				$result_list = $this->search_topics($q, $limit);
				break;
			
			case 'topic_add' :
				$result_list = $this->search_topics($q, $limit);
				
				if ($this->model('topic')->has_topic($q[0]))
				{
					$data[] = array(
						'exist' => 1
					);
				}
				else
				{
					$data[] = array(
						'exist' => 0
					);
				}

				break;
			
			case 'question' :
				$result_list = $this->search_questions($q, $limit);
				break;
		}
		
		if ($result_list)
		{
			foreach ($result_list as $result_info)
			{
				$result = $this->prase_result_info($result_info);
			
				if (is_array($result))
				{
					$data[] = $result;
				}
			}
		}

		
		return $data;
	}

	public function prase_result_info($result_info)
	{
		if (isset($result_info['last_login']))
		{
			$result_type = 3;
			
			$sno = $result_info['uid'];
				
			$user_info = $this->model('account')->get_users_by_uid($result_info['uid'], true);
				
			$name = $user_info['user_name'];
			$url = get_js_url('/people/' . $name);
			$detail = array(
				'avatar_file' => get_avatar_url($result_info['uid'], 'mid', $user_info['avatar_file']),	// 头像
				'signature' => $user_info['signature'],	// 签名
				'reputation' =>  $user_info['reputation'],	// 威望
				'agree_count' =>  $user_info['agree_count'],	// 赞同
				'thanks_count' =>  $user_info['thanks_count'],	// 感谢
			);
		}
		else if ($result_info['topic_id'])
		{
			$result_type = 2;
			$sno = $result_info['topic_id'];
			$url = get_js_url('/topic/' . $result_info['topic_id']);
			$name = $result_info['topic_title'];
			
			if (!$result_info['topic_pic'])
			{
				$result_info['topic_pic'] = G_STATIC_URL . '/common/topic-mid-img.jpg';
			}
			else
			{
				$result_info['topic_pic'] = get_setting('upload_url').'/topic/'.str_replace(AWS_APP::config()->get('image')->topic_thumbnail['min']['w'].'_'.AWS_APP::config()->get('image')->topic_thumbnail['min']['h'], AWS_APP::config()->get('image')->topic_thumbnail['max']['w'].'_'.AWS_APP::config()->get('image')->topic_thumbnail['max']['h'], $result_info['topic_pic']);
			}
			
			$detail = array(
				'topic_pic'=> $result_info['topic_pic'],	
				'topic_id' => $result_info['topic_id'],	// 话题 ID
				'focus_count' => $result_info['focus_count'],
				'discuss_count' => $result_info['discuss_count']	// 讨论数量
			);
		}
		else if ($result_info['question_id'])
		{
			$result_type = 1;
			$sno = $result_info['question_id'];
			$url = get_js_url('/question/' . $result_info['question_id']);
			$name = $result_info['question_content'];
			$detail = array(
				'best_answer' => $result_info['best_answer'],	// 最佳回复 ID
				'answer_count' => $result_info['answer_count'],	// 回复数
				'comment_count' => $result_info['comment_count'],
				'focus_count' => $result_info['focus_count']
			);
		}
		
		if ($name)
		{
			return array(
				'uid' => $result_info['uid'], 
				'type' => $result_type, 
				'url' => $url, 
				'sno' => $sno, 
				'name' => $name, 
				'detail' => $detail
			);
		}
	}
}